<?php
require_once(dirname(__FILE__).'/OAuth.php');
require_once(dirname(__FILE__).'/Constants.php');
require_once(dirname(__FILE__).'/Config.php');
require_once(dirname(__FILE__).'/Util.php');
require_once(dirname(__FILE__).'/AppliInfo.php');

class MixiDataAPI {
    function get ($user, $app_id, $api, $options = array()) {
        if (!$user || !is_numeric($user))
            return;

        // Establish an OAuth consumer based on our admin 'credentials'
        $app_info = new AppliInfo($app_id);
        $consumer = new OAuthConsumer($app_info->consumer_key, $app_info->consumer_secret, NULL);

        // Setup OAuth request based our previous credentials and query
        $base_feed = 'http://api.mixi-platform.com/os/0.8'.$api;
        $params = array(
            'xoauth_requestor_id' => $user,
            'format'              => 'atom',
            'count'               => 200,
        );
        $params  = array_merge($options, $params);
        $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'GET', $base_feed, $params);

        // Sign the constructed OAuth request using HMAC-SHA1
        $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);

        // Make signed OAuth request to the Contacts API server
        $url = $base_feed . '?' . MixiDataAPI::implode_assoc('=', '&', $params);

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');

        $auth_header = $request->to_header();
        if ($auth_header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
        }

        $content = curl_exec($curl);
        $header  = curl_getinfo($curl);
        $error   = curl_errno($curl);
        $er_mess = curl_error($curl);

        if ($header['http_code'] != 200 || !$content) {
# trigger_error("API: $url\n".'HTTP CODE: '.$header['http_code']."\n$content", E_USER_INFO);
        }
        curl_close($curl);

        @$obj = simplexml_load_string($content)->entry;
        if ($obj == FALSE)
            return array();
        $result = array();
        foreach($obj as $a) {
            $result[] = to_array($a->content);
        }

        return $result;
    }
    
    /**
     * Joins key:value pairs by inner_glue and each pair together by outer_glue
     * @param string $inner_glue The HTTP method (GET, POST, PUT, DELETE)
     * @param string $outer_glue Full URL of the resource to access
     * @param array $array Associative array of query parameters
     * @return string Urlencoded string of query parameters
     */
    function implode_assoc($inner_glue, $outer_glue, $array) {
        $output = array();
        foreach($array as $key => $item) {
            $output[] = $key . $inner_glue . urlencode($item);
        }
        return implode($outer_glue, $output);
    }
}
